#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

import unittest

from jutebag import BloomFilter,BloomHelper

class   BloomFilter01TestCase(unittest.TestCase) :
    def setUp(self) :
        pass
        ## setUp()

    def tearDown(self) :
        pass
        ## tearDown()

    def testComputeBucketsAndK(self) :
        for max_false_positive_probability in (0.1, 0.01, 0.001, 0.0001, 0.00001) :
            K, buckets_per_element = BloomHelper.computeBucketsAndK( max_false_positive_probability )
            print 'MAX FalsePositive: %.5f  K=%2d  Buckets=%2d' % (max_false_positive_probability, K, buckets_per_element)
        ## testComputeBucketsAndK()

    def testSerialize1(self) :
        bf = BloomFilter(num_elements = 9, buckets_per_element = 15)

        words = ('one','two','three','four','five','six','seven','eight','nine','ten')
        for w in words :
            bf.add(w)
        for w in words :
            self.assertTrue(w in bf)

        serialized = bf.serialize()
        bf2 = bf.deserialize( serialized )

        for w in words :
            self.assertTrue(w in bf2)

        self.assertEqual(bf.hash_times, bf2.hash_times)
        self.assertEqual(bf.bit_vector, bf2.bit_vector)

        ## testSerailize1()

    def testSimple1(self) :
        bf = BloomFilter(num_elements = 9, buckets_per_element = 15)

        #print str(bf)
        bf.add('a')
        #print str(bf)

        self.assertTrue('a' in bf)
        self.assertFalse('b' in bf)
        ## testSimple1()

    def testSimple2(self) :
        bf = BloomFilter(num_elements = 9, buckets_per_element = 9)

        #print str(bf)
        bf.add('a')
        #print str(bf)

        self.assertTrue('a' in bf)
        self.assertFalse('b' in bf)
        ## testSimple2()

    def testSimple3(self) :
        bf = BloomFilter(num_elements = 9, buckets_per_element = 5)

        #print str(bf)
        bf.add('a')
        #print str(bf)

        self.assertTrue('a' in bf)
        self.assertFalse('b' in bf)
        ## testSimple3()

    ## class BloomFilter01TestCase


if __name__ == '__main__' :
    unittest.main()

